package com.tarena.lbs.user.controller;

import com.tarena.lbs.base.protocol.exception.BusinessException;
import com.tarena.lbs.base.protocol.model.Result;
import com.tarena.lbs.base.protocol.pager.PageResult;
import com.tarena.lbs.business.api.pojo.BusinessDto;
import com.tarena.lbs.common.encoder.JwtEncoder;
import com.tarena.lbs.common.enums.PassportResultEnum;
import com.tarena.lbs.common.enums.Roles;
import com.tarena.lbs.common.principle.UserPrinciple;
import com.tarena.lbs.common.utils.AuthenticationContextUtils;
import com.tarena.lbs.user.api.pojo.dto.AdminDto;
import com.tarena.lbs.user.pojo.param.AdminParam;
import com.tarena.lbs.user.pojo.po.AdminPO;
import com.tarena.lbs.user.pojo.query.AdminQuery;
import com.tarena.lbs.user.pojo.query.UserQuery;
import com.tarena.lbs.user.pojo.vo.AdminVO;
import com.tarena.lbs.user.pojo.vo.UserGroupVO;
import com.tarena.lbs.user.pojo.vo.UserVO;
import com.tarena.lbs.user.service.AdminService;
import com.tarena.lbs.user.service.DubboService;
import com.tarena.lbs.user.service.UserService;
import com.tarena.lbs.user.service.AdminService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import lombok.val;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * 处理和后台用户相关的业务接口
 */

@Slf4j
@Api(tags = "后台用户管理")
@RestController
public class AdminController {

    @Autowired
    private DubboService  dubboService;

    @Autowired
    private UserService userService;

    @Autowired
    private AdminService  adminService;

    private final JwtEncoder jwtEncoder;

    public AdminController(JwtEncoder jwtEncoder) {
        this.jwtEncoder = jwtEncoder;
    }

    @ApiOperation("用户详情")
    @GetMapping("/admin/basic/role/detail")
    public Result<AdminVO> detail(@RequestHeader("Authorization") String token) throws BusinessException {
        UserPrinciple userPrinciple= (UserPrinciple) jwtEncoder.getLoginFromToken(token, UserPrinciple.class);
        AdminVO adminVO =adminService.detail(userPrinciple.getId());
        return new Result<>(adminVO);
    }
    //查询后台管理的 账号列表
    @ApiOperation("账号列表")
    @PostMapping("/admin/basic/role/list")
    public Result<PageResult<AdminVO>> pageList(@RequestBody AdminQuery query)throws BusinessException{
        log.debug("查询账号列表参数:{}", query);
        PageResult<AdminVO> adminVO = adminService.adminList(query);
        return new Result("0","success",adminVO);

    }

    @ApiOperation("账号保存")
    @PostMapping("/admin/basic/role/add")
    public Result<Void> save(@RequestBody AdminParam param,@RequestHeader("Authorization") String token)throws BusinessException{
        log.debug("保存账号参数:{}", param);
        UserPrinciple userPrinciple = (UserPrinciple) jwtEncoder.getLoginFromToken(token, UserPrinciple.class);
        Integer businessId = userPrinciple.getId();
        Integer accountType = adminService.getType(businessId);
        if (accountType!=0){
            log.debug("权限不足");
            throw new BusinessException(PassportResultEnum.INSUFFICIENT_PERMISSION);
        }
        Integer accountType1 = param.getAccountType();
        if (accountType1!=0){
            Integer id = param.getBusinessId();
            BusinessDto count =dubboService.selectByBusinessId(id);
            log.debug("dubboService.selectByBusinessId(id) 返回的 AdminDto 对象: {}", count);
            if (count==null){
                log.debug("该商家不存在");
                throw  new BusinessException(PassportResultEnum.MERCHANT_NONE_EXIST);
            }
        }

        adminService.save(param);
        return Result.success();


//        adminService.save(param);
//        return Result.success();
    }

    @ApiOperation("用户列表")
    @PostMapping("/admin/basic/user/list")
    public Result<PageResult<UserVO>> userList(UserQuery userQuery) throws BusinessException{
        PageResult<UserVO> pageResult = userService.List(userQuery);
        return new Result("0","success",pageResult);
    }












































}
